import {seriesIntArray} from '@mall/utils/format'

import mall from "@src/store/mall";
import {getShopList, getTicketDetail} from "@mall/api/good";
import {storeBindingsBehavior} from "mobx-miniprogram-bindings";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    fields: ['curLocation']
  }],
  options: {
    addGlobalClass: true
  },
  data: {
    //定义券类型3.
    couponTypeOptions: {
      GENERAL: "通用券",
      CASH: "代金券",
      DISCOUNT: "折扣券"
    },
    weekOptions: ["", "周一", "周二", "周三", "周四", "周五", "周六", "周日"],
    //有效期类型
    timeType: null,
    availablePeriods: null,
    couponPrototype: null,
    // 门店列表
    shopList: null,
    contentHeight: 0,
    couponViewHeight: 332,
    dialogShow: false
  },
  methods: {
    open(goodsItem) {
      this.fetchData(goodsItem);
      this.setData({dialogShow: true})
    },
    fetchData(goodsItem) {
      this.setData({couponPrototype: null, availablePeriods: null, shopList: null})
      getTicketDetail(goodsItem.itemNo).then(res => {
        let couponDetail = res.data.couponDetail
        this.setData({
          availablePeriods: couponDetail.availablePeriods,
          couponPrototype: couponDetail.couponPrototype,
        })
        this.initData();
        setTimeout(res => {
          this.getCouponViewHeight();
        }, 300)
        let shopList = couponDetail.couponPrototype.shopList;
        if (shopList) {
          shopList = shopList.split(",")
        } else {
          shopList = []
        }
        this.fetchShopList(shopList);
      })
    },
    fetchShopList(shopIds) {
      //根据券获取门店信息
      let params = {}
      if (shopIds.length !== 0) {
        params = {
          idList: shopIds
        }
      }
      if (this.data.curLocation) {
        params['latitude'] = this.data.curLocation.latitude;
        params['longitude'] = this.data.curLocation.longitude;
      }
      getShopList(params).then(res => {
        this.setData({shopList: res.data})
      })
    },
    initData() {
      // 格式化有效期
      let hasTimeLimit = this.data.couponPrototype.hasTimeLimit
      let availablePeriods = JSON.parse(JSON.stringify(this.data.availablePeriods));
      if (hasTimeLimit) {
        let timeType = availablePeriods[0].timeType;
        this.setData({timeType: timeType})
        availablePeriods.forEach(item => {
          if (item.startHour < 10) item.startHour = "0" + item.startHour;
          if (item.startMinute < 10)
            item.startMinute = "0" + item.startMinute;
          if (item.endHour < 10) item.endHour = "0" + item.endHour;
          if (item.endMinute < 10) item.endMinute = "0" + item.endMinute;
          // week
          if (item.timeType === "week") {
            if (item.availablePeriod === "6,7") {
              item.weekText = "周六到周日";
            } else if (item.availablePeriod === "1,2,3,4,5") {
              item.weekText = "周一到周五";
            } else if (item.availablePeriod === "1,2,3,4,5,6,7") {
              item.weekText = "周一到周日";
            } else {
              item.weekText = "";
              item.availablePeriod.split(",").forEach(weekNumber => {
                item.weekText =
                    item.weekText + this.data.weekOptions[weekNumber];
              });
            }
            item.weekText =
                item.weekText +
                "，" +
                item.startHour +
                ":" +
                item.startMinute +
                ":" +
                "00 - " +
                item.endHour +
                ":" +
                item.endMinute +
                ":00";
          }
          // days
          if (item.timeType === "day") {
            let daySeriesArr = seriesIntArray(
                item.availablePeriod.split(",")
            );
            item.dayText = "";
            daySeriesArr.forEach(v => {
              if (v.length === 1) {
                item.dayText += v[0] + "号、";
              } else {
                item.dayText += v[0] + "-" + v[v.length - 1] + "号、";
              }
            });
            item.dayText =
                item.dayText.slice(item.dayText.length - 1) === "、" ?
                    item.dayText.slice(0, -1) + "，" :
                    item.dayText;
            item.dayText =
                item.dayText +
                item.startHour +
                ":" +
                item.startMinute +
                ":" +
                "00 - " +
                item.endHour +
                ":" +
                item.endMinute +
                ":00";
          }
        });
      }
      this.setData({availablePeriods: availablePeriods})
    },
    getCouponViewHeight() {
      let query = wx.createSelectorQuery().in(this);
      query.select('#scroll-a').boundingClientRect()
      query.selectViewport().scrollOffset()
      query.exec((res) => {
        let couponViewHeight = res[0].height;
        this.setData({couponViewHeight})
      })
    },
    //监听高度
    handleContentHeight(val) {
      this.setData({contentHeight: val.detail})
    },
    handleCallPhone(e) {
      wx.makePhoneCall({phoneNumber: e.currentTarget.dataset.phone})
    },
    handleToLocation(e) {
      let item = e.currentTarget.dataset.item
      wx.openLocation({latitude: item.latitude, longitude: item.longitude, scale: 18})
    },
  },
})
